package test.execute;

import java.util.Calendar;

import test.WorkFlowTest;

import com.cait.framework.configuration.Option;
import com.cait.framework.utils.DateHelper;
import com.cait.framework.utils.Guid;
import com.cait.workflowserver.definition.activitydefiniton.Activity;
import com.cait.workflowserver.definition.activitydefiniton.enActivityTypes;
import com.cait.workflowserver.definition.processdefinition.ProcessDef;
import com.cait.workflowserver.definition.transitiondefiniton.Transition;
 
import com.cait.workflowserver.execution.FlowExecuteContext;
import com.cait.workflowserver.execution.execinstance.ProcessInstance;
import com.cait.workflowserver.interactiveface.serviceface.ServiceApi;
import com.cait.workflowserver.runtime.Server;

import junit.framework.TestCase;

public class testExecute  extends WorkFlowTest{
	/**
	 * @param arg0
	 */
	public testExecute(String arg0) {
		super(arg0);
	}

	ProcessDef p;
	Activity a1;
	Activity a2;
	Activity a3;
	Activity a4;
	/**
	 * @throws java.lang.Exception
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
//		Option.RegOptionType("com.cait.cobble.Configuration.AppOption");
//		com.cait.cobble.Configuration.AppOption
//				.getInstance()
//				.setConfigFilePath(
//						"E:\\JavaProject\\workspace32\\Any3_Cobble\\web\\WEB-INF\\appconfig.xml");
//		com.cait.cobble.Configuration.AppOption.getInstance().Load();
		
		
		Server.getInstance().startService();
		
		//==创建流程定义===================
		p=new ProcessDef();
		p.setId(Guid.getGuid());
		p.setName("测试流程");
		Server.getInstance().getProcDefineManager().addProcessDefToMemProccessContainer(p);
		
		 a1=new Activity();
		 a1.setId(Guid.getGuid());
		 a1.setActivityType(enActivityTypes.INITIAL);
		 a1.setName("开始活动");
		 //a1.setProcDef(p);
		 p.getActivities().add(a1);
		 
		 a2=new Activity();
		 a2.setId(Guid.getGuid());
		 a2.setActivityType(enActivityTypes.AUTOMATION);
		 a2.setName("自动活动1");
		 //a2.setProcDef(p);
		 p.getActivities().add(a2);
		 
		 a3=new Activity();
		 a3.setId(Guid.getGuid());
		 a3.setActivityType(enActivityTypes.AUTOMATION);
		 a3.setName("自动活动2");
		// a3.setProcDef(p);
		 p.getActivities().add(a3);
		 
		 a4=new Activity();
		 a4.setId(Guid.getGuid());
		 a4.setActivityType(enActivityTypes.DELAY);
		 a4.setName("延时活动1");
		 a4.getDelay().setTimeCount(1);
		 
		// a3.setProcDef(p);
		 p.getActivities().add(a4);
		 
		 Activity a5=new Activity();
		 a5.setId(Guid.getGuid());
		 a5.setActivityType(enActivityTypes.COUNT);
		 a5.setName("计数器");
		 a5.getCounter().setInitValue(0);
		// a3.setProcDef(p);
		 p.getActivities().add(a5);
		 
		 
		 Transition t1=new Transition();
		 t1.setId(Guid.getGuid());
		 t1.setFromActId(a1.getId());
		 t1.setToActId(a2.getId());
		// t1.setProcDef(p);
		 p.getTransitions().add(t1);
		 
		 
		
		 
		 
		 Transition t2=new Transition();
		 
		 t2.setId(Guid.getGuid());
		 t2.setFromActId(a2.getId());
		 t2.setToActId(a3.getId());
		// t2.setProcDef(p);
		 p.getTransitions().add(t2);
		 
		 
		 Transition t3=new Transition();
		 
 	t3.setId(Guid.getGuid());
 	t3.setFromActId(a3.getId());
 	t3.setToActId(a4.getId());
		// t2.setProcDef(p);
		 p.getTransitions().add(t3);
		 
		 Transition t4=new Transition();
		 
		 t4.setId(Guid.getGuid());
		 t4.setFromActId(a4.getId());
		 t4.setToActId(a5.getId());
				// t2.setProcDef(p);
				 p.getTransitions().add(t4);
				 
				 Transition t5=new Transition();
				 
				 t5.setId(Guid.getGuid());
				 t5.setFromActId(a5.getId());
				 t5.setToActId(a2.getId());
						// t2.setProcDef(p);
						 p.getTransitions().add(t5);
		 
//		 Transtation t3=new Transtation();
//		 
//		 t3.setId(Guid.getGuid());
//		 t3.setFromActId(a3.getId());
//		 t3.setToActId(a2.getId());
//		 t3.setProcDef(p);
//		 p.getTransitions().add(t3);
		 
		 
		 
		 
		
		//com.cait.
	}

	
	public final void testExecuteActivit()
	{
		System.out.println("执行测试");
		ServiceApi api=  ServiceApi.getInstance();
		String procInstId=api.createProcessInstance(p.getId(),"我的流程实例","ccm").getId();
		api.startProcessInstance(procInstId);

		
		
		//=============延时=========================
		Calendar l=Calendar.getInstance();
		while(DateHelper.DateTimeSub(Calendar.getInstance(),l)<10)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
 


	/**
	 * @throws java.lang.Exception
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		//========================================
		Server.getInstance().stopService();
		//=================================
		super.tearDown();
		
	}

}
